<html>
<head><meta charset="utf-8"><title>const fn question · t-compiler/const-eval · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/index.html">t-compiler/const-eval</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/const.20fn.20question.html">const fn question</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="202298368"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/const%20fn%20question/near/202298368" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> XAMPPRocky <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/const.20fn.20question.html#202298368">(Jun 29 2020 at 11:44)</a>:</h4>
<p>Hey one thing I can't find a clear indication in relation to <code>const fn</code>s is how do they work when passed non <code>const</code> arguments? Do they become regular functions? I'm wondering whether I have to create run-time and const versions of APIs or if having the const version would be enough?</p>



<a name="202299256"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/const%20fn%20question/near/202299256" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/const.20fn.20question.html#202299256">(Jun 29 2020 at 11:57)</a>:</h4>
<p>const fn with non <code>const</code> arguments are called at runtime just like ordinary functions</p>



<a name="202299917"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/const%20fn%20question/near/202299917" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/const.20fn.20question.html#202299917">(Jun 29 2020 at 12:04)</a>:</h4>
<p><code>const fn</code> with <code>const</code> arguments called at runtime are also just like ordinary functions</p>



<a name="202299940"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/const%20fn%20question/near/202299940" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/const.20fn.20question.html#202299940">(Jun 29 2020 at 12:05)</a>:</h4>
<p>there's nothing inherent that makes them evaluated at compile-time</p>



<a name="202299972"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/const%20fn%20question/near/202299972" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/const.20fn.20question.html#202299972">(Jun 29 2020 at 12:05)</a>:</h4>
<p>they just <em>can</em> be evaluated at compile-time, meaning you <em>can</em> use them in a <code>const</code> to compute things</p>



<a name="202299992"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/const%20fn%20question/near/202299992" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/const.20fn.20question.html#202299992">(Jun 29 2020 at 12:05)</a>:</h4>
<p>using them outside of a <code>const</code> context they just behave like normal functions</p>



<a name="202300073"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/const%20fn%20question/near/202300073" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/const.20fn.20question.html#202300073">(Jun 29 2020 at 12:06)</a>:</h4>
<p>if you are looking for forcing const eval, have a look at the <code>const block</code> RFC: <a href="https://github.com/rust-lang/rfcs/pull/2920">https://github.com/rust-lang/rfcs/pull/2920</a></p>



<a name="202307947"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/const%20fn%20question/near/202307947" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> XAMPPRocky <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/const.20fn.20question.html#202307947">(Jun 29 2020 at 13:24)</a>:</h4>
<p>Ah so does the <code>const</code> part only apply when you’re creating a <code>const</code> value?</p>



<a name="202308238"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/const%20fn%20question/near/202308238" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/const.20fn.20question.html#202308238">(Jun 29 2020 at 13:27)</a>:</h4>
<p>yes</p>



<a name="202308281"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/const%20fn%20question/near/202308281" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/const.20fn.20question.html#202308281">(Jun 29 2020 at 13:27)</a>:</h4>
<p>I think the <code>const</code> part only means "this function explicitly allows being executed at compile time", it does not change the behavior of the given function afaik</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>